package com.five.monkey.feign.provider.lock.zk;

import com.five.monkey.feign.provider.lock.Lock;
import com.five.monkey.feign.provider.lock.LockService;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;

/**
 * @author 狻猊
 * @date 2021/10/9 2:09 下午
 */
@Component("zkLockService")
public class ZkLockService implements LockService {

    private static final String PATH = "/locks";

    private static final String SEPARATOR = File.separator;

    @Autowired
    private CuratorFramework zkClient;

    @Override
    public Lock getLock(String key) {
        return new ZkLock(new InterProcessMutex(zkClient, PATH + SEPARATOR + key));
    }
}
